//
//  CrossFormAction.cpp
//  fingeradvanture
//
//  Created by shuaibincheng on 16/5/29.
//
//
#include "TroopForm.hpp"
#include "CrossFormAction.hpp"
void CrossFormAction::train(const Vector<cocos2d::Node *> &monsters)
{
    if(!form)
        return;
    //计算向左侧移动的距离
    auto leftTranslation = form->marginLeft();
    //计算向右侧移动的距离
    auto rightTranslation = form->marginRight();
    for(auto i = 0; i < monsters.size() - 2; i++)
    {
        auto leftMoveBy = MoveBy::create(1.0f, Vec2(-leftTranslation, 0.0f));
        auto rightMoveBy = MoveBy::create(1.0f, Vec2(rightTranslation, 0.0f));
        
        auto sequence = Sequence::create(leftMoveBy, leftMoveBy->reverse(), rightMoveBy, rightMoveBy->reverse(), NULL);
        auto repeat = Repeat::create(sequence, 10);
        
        auto downMoveBy = MoveBy::create(15.0f, Vec2(0.0f, -form->getVisibleSize().height
                                                     - form->getTroopSize().height - 20));
        
        auto spawn = Spawn::create(repeat,downMoveBy, NULL);
        monsters.at(i)->runAction(spawn);
    }
    
    for(auto i = monsters.size() - 2; i < monsters.size(); i++)
    {
        auto leftMoveBy = MoveBy::create(1.0f, Vec2(-leftTranslation, 0.0f));
        auto rightMoveBy = MoveBy::create(1.0f, Vec2(rightTranslation, 0.0f));
        
        auto sequence = Sequence::create(leftMoveBy, leftMoveBy->reverse(), rightMoveBy, rightMoveBy->reverse(), NULL);
        auto repeatTrans = Repeat::create(sequence, 10);
        
        auto rotationAction = RotateBy::create(5, 360.0f);
        auto repeatRotate = Repeat::create(rotationAction, 10);
        
        auto downMoveBy = MoveBy::create(15.0f, Vec2(0.0f, -form->getVisibleSize().height
                                                     - form->getTroopSize().height - 20));

        
        auto spawn = Spawn::create(repeatTrans, repeatRotate, downMoveBy, NULL);
        
        monsters.at(i)->runAction(spawn);
    }
}